library(broom)
package ‘broom’ was built under R version 3.6.2
computer <- read_csv(here("data/computers.csv"))
computer <- computer %>%
unite(price, c(price, X1), sep = ":")
computer_scale <- computer %>%
select(hd, ram, price) %>%
mutate_if(is.numeric, scale) %>%
column_to_rownames("price")
computer_scale %>%
ggplot(aes(x = hd, y = ram)) +
geom_point()
Some points are very concentrate special in the ram lower to 0, so probably it will work but some cluster we will have more points that others
fviz_nbclust(computer_scale, kmeans, method = "wss", nstart = 25)
Registered S3 method overwritten by 'data.table':
method from
print.data.table
With Elbow method K means of 3 or 4 look acceptable
fviz_nbclust(computer_scale, kmeans, method = "silhouette", nstart = 25)
Silhouette coefficient proupose k means of 10
fviz_nbclust(computer_scale, kmeans, method = "gap_stat", nstart = 25, k.max = 12)
Clustering k = 1,2,..., K.max (= 12): .. done
Bootstrapping, b = 1,2,..., B (= 100) [one "." per sample]:
Quick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterationsQuick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterations
..
did not converge in 10 iterations
..
did not converge in 10 iterations
...
did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterationsQuick-TRANSfer stage steps exceeded maximum (= 312950)
....
did not converge in 10 iterations
.
did not converge in 10 iterations
..
did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
..
did not converge in 10 iterations
.
did not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterations
.
did not converge in 10 iterations
.
did not converge in 10 iterations
.
did not converge in 10 iterations
.
did not converge in 10 iterations
.
Quick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterations
..
did not converge in 10 iterations
..
did not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterations
...
did not converge in 10 iterations
.
Quick-TRANSfer stage steps exceeded maximum (= 312950)Quick-TRANSfer stage steps exceeded maximum (= 312950)Quick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterations
.
did not converge in 10 iterations
..
did not converge in 10 iterations
.
did not converge in 10 iterations
...
did not converge in 10 iterations
.
did not converge in 10 iterationsQuick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterations
.
Quick-TRANSfer stage steps exceeded maximum (= 312950)
...
did not converge in 10 iterations
.
did not converge in 10 iterations
. 50
.....
did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterations
..
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterations
..
did not converge in 10 iterations
..
did not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterationsQuick-TRANSfer stage steps exceeded maximum (= 312950)
...
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterations
....
did not converge in 10 iterationsdid not converge in 10 iterations
..
did not converge in 10 iterations
.
did not converge in 10 iterations
..
Quick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
...
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
..
did not converge in 10 iterations
...
did not converge in 10 iterations
..
did not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterationsdid not converge in 10 iterations
.
Quick-TRANSfer stage steps exceeded maximum (= 312950)did not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterationsdid not converge in 10 iterations
.
did not converge in 10 iterations
..
did not converge in 10 iterations
.
did not converge in 10 iterations
.. 100
We will choose 7 it is in the middle and look also good in the Silhouette method
cluster_computer <- kmeans(computer_scale, centers = 7, nstart = 25)
cluster_computer
K-means clustering with 7 clusters of sizes 1443, 315, 745, 2007, 316, 612, 821
Cluster means:
hd ram
1 0.28448917 -0.05095751
2 2.09939430 0.69997432
3 -0.48509991 -0.05095751
4 -0.91618122 -0.83102298
5 2.53457219 2.84885195
6 -0.08251343 -0.76129843
7 0.46031848 1.36972433
Clustering vector:
1499:1 1795:2 1595:3 1849:4 3295:5 3695:6 1720:7 1995:8 2225:9 2575:10
4 4 4 3 7 7 4 4 3 4
2195:11 2605:12 2045:13 2295:14 2699:15 2225:16 1595:17 2325:18 2095:19 4395:20
3 3 4 3 3 4 4 4 4 1
1695:21 2795:22 2895:23 2875:24 4195:25 1290:26 1975:27 3995:28 3095:29 3244:30
4 4 7 4 1 4 4 1 7 3
1920:31 1995:32 2595:33 2475:34 1999:35 2675:36 2325:37 3795:38 2405:39 2425:40
4 4 3 4 4 4 3 1 3 3
2895:41 3895:42 2499:43 2255:44 3495:45 2695:46 2195:47 1749:48 2399:49 1995:50
3 1 3 3 7 3 4 4 4 4
2499:51 2395:52 2995:53 2190:54 2199:55 2125:56 2045:57 3075:58 2945:59 1945:60
4 4 3 4 3 4 4 4 3 4
3990:61 1795:62 2495:63 2220:64 2195:65 1495:66 2325:67 1499:68 2199:69 3095:70
3 4 3 4 4 4 4 4 4 3
4020:71 2725:72 1695:73 3044:74 2999:75 2844:76 1975:77 3220:78 2595:79 1629:80
1 4 4 3 4 3 3 3 4 3
3399:81 2495:82 2990:83 2720:84 2595:85 1999:86 2075:87 1795:88 1395:89 3490:90
4 3 3 3 3 3 3 4 4 3
2975:91 2145:92 2420:93 2505:94 2995:95 1495:96 1999:97 1920:98 2595:99 1695:100
4 4 3 3 7 4 4 4 7 4
1995:101 2195:102 1795:103 2999:104 1395:105 2995:106 2699:107 1720:108 2045:109 2145:110
4 3 4 6 4 3 3 4 4 4
2995:111 2095:112 3895:113 2895:114 1499:115 3165:116 2525:117 2325:118 2499:119 2799:120
3 4 1 7 4 4 4 3 4 4
1795:121 2199:122 2355:123 3220:124 2420:125 2544:126 2945:127 2295:128 1495:129 2065:130
4 4 3 3 3 3 4 3 4 4
2295:131 1799:132 2220:133 2075:134 1749:135 2195:136 2255:137 2395:138 2720:139 2195:140
4 4 4 4 4 3 3 4 3 4
2225:141 4020:142 2475:143 1975:144 3995:145 2595:146 2295:147 2199:148 1975:149 3895:150
3 1 3 4 1 3 4 4 3 1
2095:151 2225:152 3599:153 2075:154 2499:155 2575:156 2999:157 2195:158 3065:159 3399:160
4 3 3 3 3 3 4 3 4 4
1995:161 1595:162 2645:163 2345:164 2795:165 1895:166 2495:167 2599:168 1595:169 1999:170
4 4 4 3 7 4 3 4 4 3
2425:171 2495:172 1795:173 2744:174 3795:175 2745:176 2399:177 2645:178 1945:179 1695:180
3 3 4 3 1 4 4 3 4 4
2195:181 2815:182 1899:183 2644:184 3795:185 2395:186 1695:187 2575:188 2695:189 2999:190
4 4 4 3 1 3 4 3 7 7
1899:191 2499:192 1999:193 2055:194 2065:195 2055:196 2099:197 1995:198 2220:199 3220:200
4 4 3 4 4 3 4 4 4 3
3995:201 2785:202 2475:203 1499:204 1595:205 2099:206 2885:207 3795:208 2295:209 3895:210
1 3 3 4 4 4 3 1 4 1
2395:211 3135:212 2420:213 2125:214 2985:215 1395:216 2295:217 2095:218 2795:219 1945:220
3 3 3 4 3 4 3 4 7 4
2635:221 2695:222 2199:223 3895:224 2599:225 2745:226 1920:227 2895:228 2199:229 2915:230
3 3 3 1 3 3 4 7 4 3
1999:231 2720:232 1999:233 2195:234 2195:235 2295:236 1595:237 1695:238 1495:239 3795:240
3 3 4 3 3 4 4 4 4 1
1699:241 1895:242 2335:243 2495:244 2575:245 2495:246 3299:247 2699:248 1695:249 2999:250
4 4 3 3 3 3 7 3 4 4
2399:251 1499:252 2395:253 1720:254 1495:255 2399:256 3035:257 2449:258 1775:259 2599:260
3 4 3 4 4 4 3 4 4 7
2995:261 2345:262 2535:263 2345:264 1899:265 2195:266 2645:267 2785:268 2799:269 2999:270
3 3 3 3 4 4 3 3 7 6
2095:271 4020:272 2595:273 2695:274 2195:275 2995:276 2195:277 2399:278 2405:279 1795:280
4 1 3 7 3 7 4 3 3 4
2225:281 1995:282 2299:283 1795:284 2545:285 2595:286 1399:287 2475:288 2595:289 1695:290
3 4 4 4 3 3 4 3 7 4
3995:291 2999:292 2495:293 2455:294 2299:295 2690:296 2155:297 2890:298 1899:299 1595:300
1 4 3 3 4 7 3 7 4 4
1990:301 1895:302 1895:303 2799:304 2590:305 1499:306 2955:307 2285:308 2475:309 3795:310
4 4 4 7 3 4 3 3 3 1
1999:311 2599:312 2895:313 3895:314 2695:315 1995:316 2399:317 2605:318 2335:319 2195:320
3 3 7 1 3 4 4 3 3 4
2425:321 2995:322 2599:323 2390:324 1690:325 1495:326 2395:327 2099:328 3090:329 2599:330
3 7 7 3 4 4 3 4 7 4
1999:331 2195:332 2190:333 2025:334 1699:335 2790:336 1795:337 2990:338 2905:339 2825:340
4 4 4 4 4 7 4 7 3 3
2390:341 2699:342 2995:343 1590:344 2345:345 2199:346 2290:347 2595:348 2515:349 2145:350
4 3 3 4 3 3 4 3 3 4
2405:351 2525:352 2595:353 2090:354 2075:355 2799:356 3299:357 2645:358 3125:359 2295:360
3 3 3 4 4 4 7 3 3 3
1775:361 2095:362 2155:363 3075:364 2815:365 1599:366 2099:367 2645:368 1795:369 2035:370
4 4 3 3 3 4 4 3 4 3
2490:371 3225:372 2690:373 2195:374 1890:375 1995:376 2695:377 2595:378 1595:379 1490:380
3 4 3 3 4 4 7 7 4 4
2125:381 2399:382 2795:383 2290:384 2395:385 2399:386 1790:387 2099:388 1395:389 1895:390
3 3 7 3 3 3 4 4 4 4
2295:391 2595:392 1899:393 1399:394 2999:395 2999:396 2449:397 1795:398 1899:399 2395:400
4 3 4 4 4 7 4 4 4 3
2399:401 1595:402 2490:403 2995:404 2890:405 2190:406 2399:407 2285:408 2590:409 2795:410
3 4 3 7 7 4 3 3 3 7
1495:411 2075:412 2090:413 2195:414 3999:415 1699:416 1995:417 1690:418 3495:419 2195:420
4 4 4 3 7 4 3 4 1 4
3090:421 2690:422 1895:423 2495:424 2405:425 1595:426 2890:427 2390:428 1690:429 2295:430
7 7 4 3 3 4 7 3 4 3
2790:431 2695:432 2595:433 1995:434 2049:435 1399:436 2805:437 2019:438 3055:439 2990:440
7 7 7 4 6 4 3 4 3 7
2605:441 2125:442 1490:443 1699:444 2495:445 2599:446 2220:447 1590:448 1499:449 2625:450
3 3 4 4 3 7 3 4 4 3
1395:451 2395:452 2420:453 2425:454 2515:455 2375:456 3105:457 2390:458 2905:459 2690:460
4 3 3 3 3 3 3 4 3 7
2695:461 1790:462 3495:463 2790:464 1790:465 1795:466 2995:467 1995:468 2599:469 3720:470
3 4 1 7 4 4 3 4 1 1
2155:471 1599:472 2525:473 2299:474 1895:475 1490:476 2035:477 1720:478 2595:479 2290:480
3 4 3 1 4 4 3 4 3 4
2455:481 1695:482 2249:483 1999:484 2455:485 2595:486 2859:487 2599:488 2855:489 2790:490
3 4 4 4 3 3 4 6 3 3
3990:491 3595:492 2195:493 3599:494 2690:495 1999:496 2990:497 1890:498 2190:499 2720:500
2 1 4 1 3 3 7 4 4 3
2145:501 2295:502 1990:503 2695:504 3149:505 1775:506 2490:507 3599:508 2690:509 3195:510
4 4 4 3 3 4 3 7 3 1
3695:511 2645:512 3090:513 1890:514 1999:515 2935:516 1990:517 2290:518 2390:519 2025:520
1 3 7 4 4 3 4 4 4 4
2095:521 2590:522 1499:523 2325:524 2099:525 2999:526 2790:527 1590:528 2499:529 2575:530
4 3 4 3 4 7 3 4 4 3
2390:531 1495:532 2895:533 2335:534 2090:535 2815:536 2195:537 1899:538 3599:539 2095:540
3 4 7 3 4 3 4 4 1 4
2690:541 2795:542 2799:543 2698:544 2390:545 2695:546 2095:547 2599:548 1825:549 1790:550
7 3 4 3 3 3 4 1 4 4
2720:551 1295:552 2990:553 1890:554 2595:555 1490:556 2075:557 3699:558 2690:559 2245:560
3 4 7 4 7 4 3 7 7 4
3999:561 2195:562 2999:563 1895:564 2295:565 2190:566 1690:567 1390:568 1890:569 2290:570
7 4 4 3 3 4 4 4 4 3
1720:571 2325:572 1699:573 2045:574 2299:575 2755:576 2145:577 3090:578 2790:579 2099:580
4 3 4 4 4 3 4 7 3 4
1998:581 2590:582 3365:583 2545:584 3265:585 2250:586 2890:587 1599:588 1825:589 2125:590
4 3 1 3 1 4 7 4 4 4
2245:591 1945:592 2890:593 1599:594 2295:595 1520:596 2745:597 2899:598 2390:599 1675:600
3 4 7 4 4 4 1 3 4 4
3149:601 2590:602 1999:603 2590:604 1449:605 1490:606 2299:607 1595:608 2495:609 1890:610
3 7 4 7 4 4 3 4 3 4
2995:611 3609:612 2790:613 2295:614 1799:615 3990:616 2999:617 2275:618 2290:619 1499:620
7 6 3 3 4 2 4 3 4 4
2555:621 2025:622 2299:623 1975:624 2109:625 3595:626 2595:627 2395:628 1899:629 2120:630
3 4 1 3 4 1 3 4 4 3
1790:631 1899:632 1990:633 1399:634 1898:635 1725:636 2595:637 1499:638 1990:639 2595:640
4 4 4 4 4 4 3 4 4 3
1395:641 2675:642 1899:643 2990:644 1925:645 4098:646 2895:647 2495:648 1995:649 1595:650
4 3 4 7 4 7 7 3 3 4
2195:651 2399:652 2390:653 2690:654 1690:655 2398:656 2775:657 1395:658 2520:659 2125:660
3 3 3 3 4 3 3 4 3 3
4398:661 2290:662 2489:663 2190:664 2599:665 3789:666 2699:667 2799:668 2499:669 2049:670
7 4 3 4 4 1 7 4 4 6
2575:671 2999:672 3495:673 2995:674 2275:675 2590:676 2975:677 1950:678 2599:679 1695:680
3 7 1 7 3 3 3 4 1 4
2455:681 1495:682 2290:683 3720:684 1795:685 2495:686 3565:687 2845:688 2025:689 3045:690
3 4 3 1 4 3 1 1 3 1
2299:691 2345:692 2390:693 2795:694 1390:695 1749:696 2690:697 1579:698 3098:699 1295:700
4 3 4 7 4 4 3 4 7 4
1945:701 2099:702 1999:703 3090:704 1890:705 1895:706 3398:707 1845:708 1795:709 2050:710
4 4 3 7 4 4 7 4 4 4
2495:711 2375:712 1690:713 3299:714 1995:715 1495:716 2999:717 3599:718 3295:719 3999:720
7 3 4 1 4 4 7 7 1 1
3025:721 1890:722 2648:723 2295:724 2199:725 2090:726 2075:727 2744:728 2425:729 1749:730
2 4 3 4 6 4 3 7 1 4
1899:731 1520:732 1795:733 1879:734 1590:735 1890:736 1720:737 1845:738 2495:739 3099:740
4 4 3 4 4 4 4 3 7 7
2990:741 2144:742 1890:743 1975:744 1945:745 4248:746 2244:747 2044:748 1690:749 2844:750
7 4 4 3 4 7 3 4 4 3
1749:751 2395:752 1890:753 2694:754 2465:755 1999:756 1599:757 2745:758 1595:759 1999:760
4 3 4 7 3 3 4 3 4 4
1939:761 1590:762 1695:763 3048:764 1599:765 2144:766 2744:767 1399:768 2015:769 2799:770
4 4 4 7 4 4 7 4 4 1
2095:771 2544:772 3948:773 2690:774 2699:775 2490:776 2244:777 1399:778 2795:779 3595:780
3 4 7 7 7 3 4 4 7 1
2045:781 2290:782 3090:783 2644:784 2348:785 2120:786 2494:787 2394:788 1845:789 3044:790
4 4 7 3 3 3 4 3 4 7
2195:791 2995:792 1829:793 2845:794 1395:795 3495:796 2890:797 2344:798 2944:799 2290:800
4 7 4 3 4 1 7 4 7 3
2799:801 2895:802 2599:803 3499:804 1509:805 2399:806 2290:807 2544:808 2295:809 1579:810
4 7 4 1 4 3 4 3 3 4
2495:811 1944:812 1769:813 3094:814 1849:815 1744:816 2744:817 1848:818 1644:819 2344:820
3 4 4 7 4 4 3 4 4 4
2694:821 2495:822 2945:823 2395:824 2590:825 1795:826 1559:827 2444:828 1794:829 3348:830
3 3 3 3 7 4 4 4 4 7
2999:831 2294:832 2095:833 1995:834 1769:835 2059:836 2990:837 2290:838 1299:839 2399:840
1 4 4 3 4 4 7 3 4 3
3789:841 2325:842 3495:843 2644:844 2644:845 1694:846 1990:847 2225:848 1295:849 2144:850
1 3 1 7 3 4 4 4 4 4
1499:851 2294:852 3695:853 2099:854 2794:855 2590:856 1795:857 1539:858 2090:859 2344:860
4 3 1 4 7 3 4 4 4 3
2195:861 1879:862 1975:863 2390:864 3720:865 1895:866 1999:867 2595:868 2620:869 2344:870
3 4 4 4 1 4 4 3 3 3
1990:871 2094:872 1945:873 2190:874 2299:875 2444:876 2175:877 2044:878 2244:879 2125:880
4 4 3 4 4 3 3 4 4 3
2845:881 3899:882 1649:883 2595:884 3144:885 1999:886 2899:887 3090:888 2094:889 1895:890
3 7 4 3 7 4 4 7 4 4
2999:891 2994:892 1495:893 1395:894 2675:895 2394:896 2590:897 2690:898 1990:899 2190:900
1 7 4 4 3 4 7 7 4 3
3895:901 2999:902 2890:903 1799:904 2794:905 1899:906 2190:907 1399:908 2844:909 1559:910
2 3 7 4 3 4 4 4 7 4
2995:911 2325:912 1599:913 2299:914 1865:915 1990:916 1994:917 2109:918 1948:919 1575:920
7 4 4 3 4 4 4 4 4 4
2690:921 1449:922 2690:923 1695:924 2145:925 2044:926 1895:927 2015:928 2490:929 1844:930
3 4 3 3 4 4 3 3 3 4
1049:931 2595:932 2390:933 1799:934 1744:935 2565:936 2744:937 2799:938 1994:939 1495:940
4 7 4 4 4 3 3 3 4 4
2045:941 3194:942 1499:943 2194:944 3609:945 2190:946 1745:947 2590:948 1639:949 2044:950
3 7 4 4 6 3 4 3 4 4
3244:951 3595:952 1595:953 2444:954 3144:955 2395:956 1944:957 2594:958 1895:959 3044:960
7 1 4 4 7 3 4 3 3 7
2225:961 1495:962 2799:963 1544:964 2344:965 2794:966 1399:967 2295:968 2195:969 2594:970
3 4 3 4 1 7 4 4 4 1
3299:971 2194:972 3499:973 2090:974 2695:975 2644:976 1695:977 2095:978 1594:979 1845:980
1 1 1 4 6 1 4 4 4 4
2044:981 4295:982 2795:983 2045:984 2544:985 2399:986 2225:987 1470:988 1575:989 2394:990
4 2 7 3 1 3 4 4 4 4
1795:991 1599:992 1299:993 4494:994 2744:995 2444:996 2090:997 2290:998 2469:999 1495:1000
4 4 4 2 7 1 1 4 3 4
[ reached getOption("max.print") -- omitted 5259 entries ]
Within cluster sum of squares by cluster:
[1] 125.46233 246.58534 36.02784 121.99691 189.37178 49.46172 168.14783
(between_SS / total_SS = 92.5 %)
Available components:
[1] "cluster" "centers" "totss" "withinss" "tot.withinss" "betweenss"
[7] "size" "iter" "ifault"
tidy(cluster_computer,
col.names = colnames(computer_scale))
glance(cluster_computer)
augment(cluster_computer, computer_scale)
max_k <- 10
k_clusters <- tibble(k = 1:max_k) %>%
mutate(
kclust = map(k, ~ kmeans(computer_scale, .x, nstart = 25)),
tidied = map(kclust, tidy),
glanced = map(kclust, glance),
augmented = map(kclust, augment, computer)
)
k_clusters
clusterings <- k_clusters %>%
unnest(glanced)
clusterings
clusterings %>%
unnest(cols = c(augmented)) %>%
filter(k <= 10) %>%
ggplot(aes(x = hd, y = ram)) +
geom_point(aes(color = .cluster)) +
facet_wrap(~ k)
With 7 the clusters look ok with 10 we have clusters with only 2 points, with 2 the partition doesn’t look to logical special for the point that have a ram of 15.